/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.desktop;

import com.android1.amarena2d.engine.Config;
import com.android1.amarena2d.desktop.AmarenaDesktopEngine;
import com.android1.amarena2d.nodes.Scene;
import com.badlogic.gdx.Files;
import org.amarena2d.examples.Example;
import org.amarena2d.examples.Examples;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ExampleStarter {

    static Config config;

    static class TestList extends JPanel {
        public TestList() {
            setLayout(new BorderLayout());

            final JList list = new JList(Examples.getNames().toArray());
            final JButton button = new JButton("Start Example");
            JScrollPane pane = new JScrollPane(list);

            DefaultListSelectionModel m = new DefaultListSelectionModel();
            m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            m.setLeadAnchorNotificationEnabled(false);
            list.setSelectionModel(m);

            list.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent event) {
                    if (event.getClickCount() == 2) button.doClick();
                }
            });

            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    final String name = (String) list.getSelectedValue();

                    Example exampleConfig = Examples.findConfig(list.getSelectedIndex());

                    config = new Config(exampleConfig.width(), exampleConfig.height());
                    config.setBaseAssetsPath("org/amarena2d/examples/scenes/assets/");
                    config.setDefaultFileType(Files.FileType.Classpath);

                    AmarenaDesktopEngine engine = new AmarenaDesktopEngine(name, config) {

                        @Override
                        public void onSurfaceCreated() {

                            final Scene scene = Examples.createScene(list.getSelectedIndex());
                            getDirector().runScene(scene);
                        }
                    };


                }
            });

            add(pane, BorderLayout.CENTER);
            add(button, BorderLayout.SOUTH);
        }
    }

    public static void main(String[] argv) {
        JFrame frame = new JFrame("Amarena2d Examples Launcher");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new TestList());
        frame.pack();
        frame.setSize(frame.getWidth(), 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }
}
